package com.dupf.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author dupf
 */
public class ItvJsonUtil {
	private static final Log log = LogFactory.getLog(ItvJsonUtil.class);
    public static ObjectMapper mapper = new ObjectMapper();
    // 序列化时null字段不输出
    static {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    /**
     * 将json字符串反序列号成对象
     * 
     * @param <T>
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T readValue(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            log.error("readValue exception", e);
            // e.printStackTrace();
        } catch (JsonMappingException e) {
            log.error("readValue exception", e);
            // e.printStackTrace();
        } catch (IOException e) {
            log.error("readValue exception", e);
            // e.printStackTrace();
        }

        return null;
    }

    /**
     * 
     * @Title: readValue
     * 
     * @author wugaoping
     * @date: 2013-12-24 下午7:30:34
     * @param json
     * @param valueTypeRef
     * @return
     * @return: T
     */
    public static <T> T readValue(String json, TypeReference<T> valueTypeRef) {
        try {
            return mapper.readValue(json, valueTypeRef);
        } catch (JsonParseException e) {
        	log.error("readValue exception", e);
            // e.printStackTrace();
        } catch (JsonMappingException e) {
        	log.error("readValue exception", e);
            // e.printStackTrace();
        } catch (IOException e) {
        	log.error("readValue exception", e);
            // e.printStackTrace();
        }

        return null;
    }

    /**
     * List<T> 转 json 保存到数据库
     */
    public static <T> String listToJson(List<T> ts) {
        String jsons = JSON.toJSONString(ts);
        return jsons;
    }

    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }

    /**
     * JSON 转 POJO
     */
    public static <T> T getObject(String pojo, Class<T> tclass) {
        try {
            return JSONObject.parseObject(pojo, tclass);
        } catch (Exception e) {
            log.error(tclass + "转 JSON 失败");
        }
        return null;
    }

    /**
     * POJO 转 JSON
     */
    public static <T> String getJson(T tResponse){
        String pojo = JSONObject.toJSONString(tResponse);
        return pojo;
    }

    /**
     * 将对象序列号json字符串
     * 
     * @param obj
     * @return
     */
    public static String writeValue(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonParseException e) {
        	log.error("writeValue exception", e);
            // e.printStackTrace();
        } catch (JsonMappingException e) {
        	log.error("writeValue exception", e);
            // e.printStackTrace();
        } catch (IOException e) {
        	log.error("writeValue exception", e);
            // e.printStackTrace();
        }

        return null;
    }

    public static String RESPONSE(String code, Object data) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        map.put("data", data);
        return writeValue(map);
    }

    public static String RESPONSE(int code, Object data) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        map.put("data", data);
        return writeValue(map);
    }

    /**
     * 返回分页信息时用到
     */
    public static String RESPONSEPAGED(String code, Object data, Integer start, Integer total, Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        map.put("data", data);
        map.put("total", total);// 总量
        map.put("limit", pageSize);// 每页显示个数
        map.put("start", start);// limit 起始位置
        return writeValue(map);
    }

    public static String RESPONSERANGE(String code, Object data, Integer start, Integer total, Integer end) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", code);
        map.put("data", data);
        map.put("total", total);// 总量
        map.put("end", end);// 每页显示个数
        map.put("start", start);// limit 起始位置
        return writeValue(map);
    }

    public static String RESPONSEPAGED(String chnId, String code, Object data, Integer start, Integer total,
        Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("chnId", chnId);
        map.put("code", code);
        map.put("data", data);
        map.put("total", total);// 总量
        map.put("limit", pageSize);// 每页显示个数
        map.put("start", start);// limit 起始位置
        return writeValue(map);
    }
}
